<!--
 * @author guojiteng
 答题考试管理
 -->
<template>
  <div class="a-rush-container">
    <el-collapse-transition>
      <div class="course-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model="listQuery.anyThing" clearable placeholder="按考试名称进行搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <div class="course-btn">
      <div class="btn-title">
        <span>考试管理</span>
      </div>
      <div class="btn-btns">
        <el-button style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <el-button style="margin-left: 10px;" @click="handleSelectPlatform">选取SaaS平台考试 </el-button>
      </div>
    </div>
    <div v-loading="listLoading" class="course-list">
      <v-table :list="rushList" tag="examManagement" :list-loading="listLoading" :source-arr="sourceArr" :filter-column="filtercolumn">
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <div class="item-btns">
              <el-button
                v-if="scope.row.isForce !== 'SF01'"
                size="small"
                type="text"
                @click="handleStatus(scope.row)"
              >{{ scope.row.status === 'ZT01' ? '禁用' : '启用' }}
              </el-button>
              <el-button
                v-if="scope.row.isForce !== 'SF01' && scope.row.isForce !== 'SF02'"
                type="text"
                size="small"
                @click="handleEdit(scope.row)"
              >编辑</el-button>
              <el-button
                v-if="scope.row.status == 'ZT02' && scope.row.isForce !== 'SF01'"
                type="text"
                size="small"
                @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-button
                v-if="scope.row.isForce === 'SF01'"
                type="text"
                size="mini"
                @click="handleSort(scope.row)"
                >修改排序</el-button
              >
            </div>
          </template>
        </el-table-column>
        <div v-if="rushList.length <= 0" class="nodata">
          <img src="../images/nodata.png">
        </div>
      </v-table>
    </div>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <rush-bank v-if="showbank" :id="vid" type="CGLX01" @cancel="handleCancel" />
    <el-dialog
      title="修改排序"
      width="30%"
      :visible.sync="sortDialog"
      @close="cancelDialog"
      v-el-drag-dialog
    >
      <el-input
        v-model="form.sort"
        placeholder="请输入排序值"
        type="number"
      ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="cancelDialog">取 消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="handelUpdateSort">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import RushBank from '@/components/Bank'
import { queryPage, updateStatus, del, updateSort } from './../api/manage'
import { mapGetters } from 'vuex'
import rushOne from './../images/rush-one.png'
import rushTwo from './../images/rush-two.png'
import rushThree from './../images/rush-three.png'
import rushFour from './../images/rush-four.png'
import defaultRushImg from './../images/rush-default.png'
import rushFive from './../images/rush-five.png'
import rushSix from './../images/rush-six.png'
import rushSeven from './../images/rush-seven.png'
import rushEight from './../images/rush-eight.png'
import permission from '@/directive/permission/index.js' // 权限判断指令
import vTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'OnlineExamIndex',
  components: { Pagination, RushBank, vTable },
  directives: { permission, elDragDialog },
  data() {
    return {
      defaultRushImg: 'this.src="' + defaultRushImg + '"',
      listLoading: false,
      list: null,
      sourceArr: ['imgIdUrl'],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        anyThing: ''
      },
      checkList: [],
      rushList: [],
      view: '/API-DOCUMENT/document/file/download?id=',
      vid: '',
      showbank: false,
      link: '/rush/answer?id=',
      form: {
        id: '',
        sort: ''
      },
      sortDialog: false
    }
  },
  computed: {
    ...mapGetters(['sidebar'])
  },
  created() {
    this.getList()
  },
  methods: {
    handleSelectPlatform() {
      this.$router.push({
        path: '/skill/exam/SelectCourse4platform',
        name: 'SelectCourse4platformExamination',
        query: {}
      })
    },
    filtercolumn(scope, item) {
      if (item === 'subjectNum') {
        return `${scope.row.subjectNum || '0'}`
      } else if (item === 'isRecommend') {
        return `${scope.row.isRecommend === '1' ? '是' : '否'} / ${scope.row.isDiscuss === 1 ? '是' : '否'}`
      } else {
        return scope.row[item]
      }
    },
    /**
     * 格式化考试背景图片
     * @param index
     * @returns {*}
     */
    filterRushImg(imgId, index) {
      if (imgId) {
        if (index % 4 === 0) {
          return rushFive
        }
        if (index % 4 === 1) {
          return rushSix
        }
        if (index % 4 === 2) {
          return rushSeven
        }
        if (index % 4 === 3) {
          return rushEight
        }
      } else {
        if (index % 4 === 0) {
          return rushOne
        }
        if (index % 4 === 1) {
          return rushTwo
        }
        if (index % 4 === 2) {
          return rushThree
        }
        if (index % 4 === 3) {
          return rushFour
        }
      }
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleReset() {
      this.listQuery.anyThing = ''
      this.listQuery.page = 1
      this.getList()
    },

    handleClassify() {
      this.classifyDialogVisible = true
    },

    handleCreate() {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'c' }})
    },
    handleEdit(row) {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'u', i: row.id }})
    },

    handleStatus(row) {
      const state = row.status === 'ZT01' ? 'ZT02' : 'ZT01'
      let msg = ''
      if (row.status === 'ZT01') {
        msg = '禁用'
      } else {
        msg = '启用'
      }
      this.$confirm('确定' + msg + '该考试吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            updateStatus({ id: row.id, status: state })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: msg + '成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {})
    },

    handleDelete(row) {
      this.$confirm('此操作将永久删除该考试, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            del(row.id)
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch()
    },
    /**
     * 查看前端地址
     * /rush/answer?id=Zffd5f2cd-8c3d-459d-8c8b-e4b30b6c6212
     */
    handleAddress(id) {
      this.$confirm(`链接地址为：${this.link}${id}<span style="color:red">注意：该链接只对已发布数据有效</span>`, '提示', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '复制链接',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const input = document.createElement('input')
          input.setAttribute('readonly', 'readonly')
          input.setAttribute('value', this.link + id)
          document.body.appendChild(input)
          input.select()
          if (document.execCommand('copy')) {
            document.execCommand('copy')
            this.$message({
              title: '成功',
              message: '复制成功！',
              type: 'success',
              duration: 1500
            })
          }
          document.body.removeChild(input)
        })
        .catch(() => {})
    },
    handleBank(row) {
      this.vid = row.id
      this.showbank = true
    },

    handleCancel() {
      this.showbank = false
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        name: this.listQuery.anyThing
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.listLoading = false
            this.rushList = response.data.data.rows
            this.total = response.data.data.total
          })
          .catch((error) => {
            this.listLoading = false
            reject(error)
          })
      })
    },

    // 修改排序
    handelUpdateSort() {
      if (this.form.sort === '') {
        this.$message({
          message: '请输入排序值',
          type: 'warning'
        })
        return
      }
      const obj = Object.assign({}, this.form)
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      updateSort(obj).then(res => {
        if(res.data.code === '200') {
          loading.close()
          this.sortDialog = false
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getList()
        } else {
          loading.close()
          this.$message({
            message: '修改失败',
            type: 'error'
          })
        }
      }).catch(() => {
        loading.close()
      })
    },

    // 强制下发数组修改排序
    handleSort(row) {
      console.log(row, '482');
      this.form.id = row.id
      this.sortDialog = true
    },

    // 取消修改排序
    cancelDialog() {
      this.sortDialog = false
      for (let k in this.form) {
        this.form[k] = ''
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
$dark_gray: #bfced6;
.a-rush-container {
  padding: 10px;
  .course-search {
    background-color: #fff;
    padding: 0 20px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .course-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .course-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }
  .course-btn {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-bottom: 10px;
    margin-top: 10px;
    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }
  .course-list {
    min-height: 300px;
    .el-row {
      .el-col-5 {
        width: 20%;
      }
    }
    .course-list-item {
      background-color: #ffffff;
      padding: 10px 10px 0;
      margin-right: 10px;
      margin-bottom: 10px;
      box-shadow: 0 5px 10px rgba(254, 90, 97, 0.05);
      position: relative;

      .item-img {
        width: 100%;
        height: 150px;
        overflow: hidden;
        position: relative;
        .title {
          width: 60%;
          font-size: 12px;
          color: #fff;
          position: absolute;
          top: 10px;
          left: 10px;
        }
        .bg {
          position: absolute;
          width: 100%;
          height: 100%;
          z-index: 0;
          transition: all 0.2s linear;
          &:hover {
            transform: scale(1.1, 1.1);
            filter: contrast(130%);
          }
        }
        .item-bg-img {
          position: absolute;
          right: 10px;
          bottom: 10px;
          width: 60px;
          height: 60px;
          border-radius: 50%;
        }
        &.o {
          height: 130px;
        }
        // .pub {
        //   position: absolute;
        //   top: 0;
        //   left: 0;
        //   right: 0;
        //   bottom: 0;
        //   background: rgba(0, 0, 0, 1);
        //   opacity: 0.5;
        //   z-index: 1;
        //   span {
        //     font-size: 12px;
        //     color: #ffffff;
        //     position: absolute;
        //     top: 50%;
        //     left: 50%;
        //     transform: translate(-50%, -50%);
        //   }
        // }
      }
      .item-mark {
        font-size: 12px;
        margin: 5px 0;
        line-height: 1.5;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(166, 166, 166, 1);
      }
      .item-type {
        height: 20px;
        line-height: 20px;
        .type-gate {
          margin-right: 5px;
          img {
            width: 40px;
          }
        }
        .type-exam {
          img {
            width: 40px;
          }
        }
        .type-num {
          color: #333333;
          font-weight: 400;
          font-size: 12px;
          float: right;
        }
      }
      .item-btns-bottom {
        display: flex;
        justify-content: space-around;
        position: relative;
        .el-button {
          //flex: 1;
          padding-bottom: 10px;
          padding-top: 0;
          span {
            font-size: 13px;
            font-weight: 500;
          }
        }
      }
      .item-btns {
        display: flex;
        margin-top: 10px;
        position: relative;
        justify-content: space-around;
        .pub {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 1);
          opacity: 0.5;
          z-index: 1;
          span {
            font-size: 12px;
            color: #ffffff;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
          }
        }
        .el-button {
          //flex: 1;
          span {
            font-size: 13px;
            font-weight: 500;
          }
        }
        &:before {
          content: '';
          position: absolute;
          top: 0;
          left: -10px;
          right: -10px;
          height: 1px;
          background-color: #e2e3e8;
        }
      }
    }
  }
  .pagination-container {
    background: none;
  }
}
</style>
